//index.js
const db = wx.cloud.database()
const app = getApp()
const _ = db.command
Page({
  data: {
    nowMore: '',
    Acitivity:[],
    Acitivity1:[],
    Acitivity2:[],
    Acitivity3:[],
    Acitivity4:[],
    SelectTab : '体育活动',
    SelectTime : 'ing',
    confirmxueyue: [],
    confirmnianji: [],
    animationData: '',
    tipHidden: true,
    show: false,
    gd1List2: [
      { name: "不限", isChecked2: false },

      { name: "人文学院", isChecked2: false },

      { name: "艺术学院", isChecked2: false },

      { name: "外国语学院", isChecked2: false },

      { name: "经管学院", isChecked2: false },
      { name: "数信学院", isChecked2: false },
      { name: "水利学院", isChecked2: false },
      { name: "食品学院", isChecked2: false },
      { name: "工程学院", isChecked2: false },
      { name: "生科学院", isChecked2: false },
      { name: "海洋学院", isChecked2: false },
      { name: "资环学院", isChecked2: false },
      { name: "动科学院", isChecked2: false },
      { name: "兽医学院", isChecked2: false },
      { name: "园艺学院", isChecked2: false },
      { name: "林风学院", isChecked2: false },
      { name: "农学院", isChecked2: false },
      // { name: "兽医学院", isChecked2: false },

    ],
    gd1List3: [
      { name: "不限", isChecked2: false },

      { name: "大一", isChecked2: false },

      { name: "大二", isChecked2: false },

      { name: "大三", isChecked2: false },

      { name: "大四", isChecked2: false },
      { name: "研究生", isChecked2: false },
      // { name: "水利学院", isChecked2: false },
      // { name: "食品学院", isChecked2: false },
      // { name: "工程学院", isChecked2: false },
      // { name: "生科学院", isChecked2: false },
      // { name: "海洋学院", isChecked2: false },
      // { name: "资环学院", isChecked2: false },
      // { name: "动科学院", isChecked2: false },
      // { name: "兽医学院", isChecked2: false },
      // { name: "园艺学院", isChecked2: false },
      // { name: "林风学院", isChecked2: false },
      // { name: "农学院", isChecked2: false },
      // { name: "兽医学院", isChecked2: false },

    ],
  },

  onLoad: function() {
    const that = this

    db.collection('SportActivity').get({
      success:function(res){
       
        that.setData({
          Acitivity:res.data,
          Acitivity1:res.data
        })
      }
    })
    db.collection('AcademicActivity').get({
      success:function(res){
       
        that.setData({
          Acitivity2:res.data
        })
      }
    })
    db.collection('ArtActivity').get({
      success:function(res){
       
        that.setData({
          Acitivity3:res.data
        })
      }
    })
    db.collection('AnotherAcitivity').get({
      success:function(res){
       
        that.setData({
          Acitivity4:res.data
        })
      }
    })
  },
  _addEvent: function(){
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  _deleteLotsEvent: function(){
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  _endEvent:function(){
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  
  SelectTab : function(e){
    // console.log(e)
    var SelectTab = e.currentTarget.dataset.type;
    if(SelectTab == '体育活动')
    {
      this.setData({
        Acitivity:this.data.Acitivity1,
        SelectTab 
      })
    }
   else if(SelectTab == '学术活动')
    {
      this.setData({
        Acitivity:this.data.Acitivity2,
        SelectTab 
      })
    }
    else if(SelectTab == '艺术活动')
    {
      this.setData({
        Acitivity:this.data.Acitivity3,
        SelectTab 
      })
    }
    else if(SelectTab == '其他活动')
    {
      this.setData({
        Acitivity:this.data.Acitivity4,
        SelectTab 
      })
    }



   
  },
  selectTime:function(e){
    // console.log(e);
    var SelectTime = e.currentTarget.dataset.time;
    this.setData({
      SelectTime
    })
  },
  filter: function (e) { //点击筛选事件
    var animation = wx.createAnimation({//创建动画
      duration: 1000,
      timingFunction: 'ease',
      width: 300,
      height: 800,
      top: 0,
      bottom: 0,
      right: 0,
      backgroundColor: '#fff',
      opcity: 0.5
    })

    this.animation = animation

    animation.translateX(-100 + 'vh').step() //动画效果向右滑动100vh

    this.setData({
      animationData: animation.export(),
      show: true
    })
  },
  getDemo2: function (e) {

    var that = this

    var index = e.currentTarget.dataset.index;//每一个button的索引

    var item = that.data.gd1List2[index];//每一个索引对应的内容

    item.isChecked2 = !item.isChecked2;//选中，未选中 两种状态切换

    that.setData({//更新到data

      gd1List2: that.data.gd1List2,

    });

  },
  getDemo3: function (e) {

    var that = this

    var index = e.currentTarget.dataset.index;//每一个button的索引

    var item = that.data.gd1List3[index];//每一个索引对应的内容

    item.isChecked2 = !item.isChecked2;//选中，未选中 两种状态切换

    that.setData({//更新到data

      gd1List3: that.data.gd1List3,

    });

  },
  confirmSelect() {
    var that = this
    var array3 = that.data.gd1List3
    var array = that.data.gd1List2
    var array2 = []
    var array4 = []
    for (var i = 0; i < array.length; i++) {
      if (array[0].isChecked2 == true) {
        array2.push(array[i]);
        break;
      }

      if (array[i].isChecked2 == true) {

        array2.push(array[i]);
      }
    }
    for (var i = 0; i < array3.length; i++) {
      if (array3[0].isChecked2 == true) {
        array4.push(array3[i]);
        break;
      }

      if (array3[i].isChecked2 == true) {

        array4.push(array3[i]);
      }
    }
    // var that = this

    that.setData({
      show: false,
      // gd1List2: that.data.gd1List2,
      // gd1List3: that.data.gd1List3,
      confirmnianji: array4,
      confirmxueyue: array2,
      haveConfirm: true
    });
    // console.log(this.data.confirmnianji);
  },
  reverseSelect() {
    var that = this
    var array1 = that.data.gd1List2
    var array2 = that.data.gd1List3
    for (var i = 0; i < array1.length; i++) {
      if (array1[i].isChecked2 == true) {
        array1[i].isChecked2 = false
      }
    }
    for (var i = 0; i < array2.length; i++) {
      if (array2[i].isChecked2 == true) {
        array2[i].isChecked2 = false
      }
    }
    that.setData({
      gd1List2: array1,
      gd1List3: array2
    })
  },
  More : function(e){

  

    // const index=e.target.dataset.index
    // console.log(index)

    // console.log(this.data.tipHidden[index])
    // this.setData({
    //   [`tipHidden[${index}]`] : !this.data.tipHidden[index],
    //   nowMore:index
    // },()=>{
    //   console.log(this.data.tipHidden[index])
    // }
    // )
    if (this.data.tipHidden == false) {
      this.setData({
        tipHidden: true
      })
    }
    else {
      this.setData({
        tipHidden: false,
        nowMore: e.currentTarget.dataset.topic
      })
    }
    
  },
  gotodetail:function(e)
  {

      const that = this
      const index = e.target.dataset.index
      console.log(index)
      wx.navigateTo({
        url: '../detail/detail',
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        },
        success: function(res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: that.data.Acitivity[index]})
        }
      })

  },
  collect:function(e)
  {
    const index = e.currentTarget.dataset.index
    const id = this.data.Acitivity[index]._id



    db.collection('user').where({
      _openid:app.globalData.openid
    }).update({
      // data 传入需要局部更新的数据
      data: {
        // 表示将 done 字段置为 true
        ['publish.Interest']:_.push(id)
      },
      success: function(res) {
        console.log(res)
      }
    })
    
  }

})
